package com.yuxin.tongyi.lsll.select;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuxin.tongyi.lsll.R;
import com.yuxin.tongyi.lsll.common.Constants;
import com.yuxin.tongyi.lsll.pojo.ClassInfo;
import com.yuxin.tongyi.lsll.pojo.Event;
import com.yuxin.tongyi.lsll.pojo.EventInfo;
import com.yuxin.tongyi.lsll.pojo.EventInfos;
import com.yuxin.tongyi.lsll.pojo.NodeEvent;
import com.yuxin.tongyi.lsll.select.ShowNodeEventDialog.CallBackInterface;
import com.yuxin.tongyi.lsll.sub.BaseActivity;
import com.yuxin.tongyi.lsll.sub.CaremaPhotoUploadActivity;
import com.yuxin.tongyi.lsll.thread.GetClassDataThread;
import com.yuxin.tongyi.lsll.thread.GetEventDataThread;
import com.yuxin.tongyi.lsll.util.PubUtil;

/**
 * @author daijin'ge
 * @date 2012-4-11
 * 处理事件数据
 */

public class ShowDataActivity extends BaseActivity {
	/** 二级列表适配器 */
	private ExpandableListAdapter mAdapter;
	/** 事件总数据 */
	private EventInfos eventInfos;
	/** 一级事件集合 */
	private List<EventInfo> eventInfo;
	/** 二级事件集合 */
	private List<List<Event>> childrenList;
	private ExpandableListView expandablelistView;
	private LayoutInflater layoutInflater = null;
	private MyHandler myHandler;
	private GetEventDataThread gedt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.expandable_activity);
		
		initialData();
		
		modifyDefaultForm();
		
		expandablelistView.setOnChildClickListener(new EventChildOnClick());
				
		new Thread(gedt).start();
		PubUtil.showProgressDialog(ShowDataActivity.this, Constants.MSG_LOADING, Constants.MSG_WAIT);
	}
	
	/**
	 *  初始化数据
	 */
	public void initialData(){
		myHandler = new MyHandler();
		gedt = new GetEventDataThread(this, myHandler);
		childrenList = new ArrayList<List<Event>>();
		expandablelistView = (ExpandableListView) findViewById(R.id.enpadable_layout_list);
	}

	/**
	 * 修改ExpandableListView的默认样式
	 */
	public void modifyDefaultForm(){
		// 去掉默认箭头
		expandablelistView.setGroupIndicator(null);
		//设置子节点的分割线的样式
		expandablelistView.setChildDivider(getResources().getDrawable(R.drawable.feed_divider));
	}
	
	/**
	 * 设置子节点的点击事件 
	 */
	private class  EventChildOnClick implements OnChildClickListener{
		@Override
		public boolean onChildClick(ExpandableListView parent, View v,
				int groupPosition, int childPosition, long id) {
			//初始化三级事件列表的对话框
			ShowNodeEventDialog sned = new ShowNodeEventDialog(ShowDataActivity.this,
					childrenList.get(groupPosition).get(childPosition),eventInfo.get(groupPosition));
			sned.show();
			//需要在此Activity中返回，所以在这里定义回调函数的内容,当单击三级事件时，调用该方法
			sned.setInterface(new CallBackInterface(){
				@Override
				public void setReturnActivity(EventInfo ei,Event ne,NodeEvent nodeEvent) {
					Intent i = new Intent();
					i.setClass(ShowDataActivity.this, CaremaPhotoUploadActivity.class);
					i.putExtra("eventInfo", ei);
					i.putExtra("Event", ne);
					i.putExtra("nodeEvent", nodeEvent);
					setResult(RESULT_OK, i);
					ShowDataActivity.this.finish();
				}
			});
			return true;
		}
	}
	 
	
	private class MyHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Bundle bundle = msg.getData();
			switch (msg.what) {
			case Constants.TH_EVENT_ON:
				eventInfos = (EventInfos) bundle
						.getSerializable(Constants.STREAM_EVENT_OBJECT);
				eventInfo = eventInfos.getEventInfo();
				for (int i = 0; i < eventInfo.size(); i++) {
					List<Event> event = eventInfo.get(i).getEvent();
					childrenList.add(event);
				}
				mAdapter = new MyExpandableListAdapter(ShowDataActivity.this);
				expandablelistView.setAdapter(mAdapter);
				PubUtil.closeProgressDialog();
				break;
			case Constants.TH_EVENT_OFF:
				PubUtil.showToastLong(ShowDataActivity.this,
						Constants.MSG_READCACHE);
				eventInfos = (EventInfos) bundle
						.getSerializable(Constants.STREAM_EVENT_OBJECT);
				eventInfo = eventInfos.getEventInfo();
				for (int i = 0; i < eventInfo.size(); i++) {
					List<Event> event = eventInfo.get(i).getEvent();
					childrenList.add(event);
					mAdapter = new MyExpandableListAdapter(ShowDataActivity.this);
					expandablelistView.setAdapter(mAdapter);
				}
				PubUtil.closeProgressDialog();
				break;
			}
		}
	}
	
	private class MyExpandableListAdapter extends BaseExpandableListAdapter {
		Activity activity;

		public MyExpandableListAdapter(Activity activity) {
			this.activity = activity;
			layoutInflater = (LayoutInflater) activity
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return childrenList.get(groupPosition).get(childPosition);
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = (View) layoutInflater.inflate(R.layout.expand_child_layout, null);
				holder.fourmText = (TextView) convertView
						.findViewById(R.id.field_child_listitem);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.fourmText.setText(getChild(groupPosition, childPosition).toString());
			return convertView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {

			return childrenList.get(groupPosition).size();
		}

		@Override
		public Object getGroup(int groupPosition) {
			return eventInfo.get(groupPosition).getFieldName();
		}

		@Override
		public int getGroupCount() {
			return eventInfo.size();
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = (View) layoutInflater.inflate(
						R.layout.expand_parent_layout, null);
				holder.imageView = (ImageView) convertView
						.findViewById(R.id.field_expandle_image);
				holder.fourmText = (TextView) convertView
						.findViewById(R.id.field_group_listitem);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.fourmText.setText(getGroup(groupPosition).toString());
			holder.imageView.setBackgroundResource(R.drawable.arrow_right);
			// 如果展开，更换箭头位置
			if (isExpanded) {
				holder.imageView.setBackgroundResource(R.drawable.arrow_down);
			}
			return convertView;
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}
	}

	class ViewHolder {
		ImageView imageView;
		TextView fourmText;
	}

}
